package com.jxbeidou.system.setting.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jxbeidou.core.cache.CacheService;
import com.jxbeidou.core.constents.CacheConstants;
import com.jxbeidou.core.enums.ResultCode;
import com.jxbeidou.core.exception.Asserts;
import com.jxbeidou.core.exception.ServerException;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.mybatis.service.impl.BaseServiceImpl;
import com.jxbeidou.core.mybatis.convert.PageResultConvert;
import com.jxbeidou.system.setting.convert.IgnoreUrlConvert;
import com.jxbeidou.system.setting.entity.IgnoreUrlDO;
import com.jxbeidou.system.setting.query.IgnoreUrlQuery;
import com.jxbeidou.system.setting.vo.IgnoreUrlVO;
import com.jxbeidou.system.setting.mapper.IgnoreUrlMapper;
import com.jxbeidou.system.setting.service.IgnoreUrlService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 允许访问URL表
 *
 * @author 杜晓祥 
 * @since 1.0.0 2023-12-14
 */
@Service
@AllArgsConstructor
@Slf4j
public class IgnoreUrlServiceImpl extends BaseServiceImpl<IgnoreUrlMapper, IgnoreUrlDO> implements IgnoreUrlService {

    @Resource
    private CacheService cacheService;

    @Override
    public PageResult<IgnoreUrlVO> page(IgnoreUrlQuery query) {
        IPage<IgnoreUrlDO> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        PageResult<IgnoreUrlVO> result = PageResultConvert.convert(page, entityList -> IgnoreUrlConvert.INSTANCE.convertList(entityList));
        return result;
    }

    private LambdaQueryWrapper<IgnoreUrlDO> getWrapper(IgnoreUrlQuery query){
        LambdaQueryWrapper<IgnoreUrlDO> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotEmpty(query.getName()), IgnoreUrlDO::getName, query.getName());
        wrapper.eq(StringUtils.isNotEmpty(query.getUrl()), IgnoreUrlDO::getUrl, query.getUrl());
        return wrapper;
    }

    @Override
    public void save(IgnoreUrlVO vo) {
        try {
            IgnoreUrlDO entity = IgnoreUrlConvert.INSTANCE.convert(vo);
            boolean bl = save(entity);
            if (bl) {
                cacheService.sAdd(CacheConstants.SYS_LOGIN_IGNORE_LIST);
            }
        } catch (Exception e) {
            log.error("新增白名单失败");
            throw new ServerException(ResultCode.SAVE_DEFAULT_FAILED);
        }

    }

    @Override
    public void update(IgnoreUrlVO vo) {
        Asserts.failIsNull(vo, "");
        Asserts.failIsNull(vo.getId(), "");
        IgnoreUrlDO old = getById(vo.getId());
        Asserts.failIsNull(old, "");
        try {
            IgnoreUrlDO entity = IgnoreUrlConvert.INSTANCE.convert(vo);
            updateById(entity);
            cacheService.sRemove(CacheConstants.SYS_LOGIN_IGNORE_LIST, old.getUrl());
            cacheService.sAdd(CacheConstants.SYS_LOGIN_IGNORE_LIST, entity.getUrl());
        } catch (Exception e) {
            log.error("白名单修改失败, message:[{}]", e.getMessage());
            throw new ServerException("");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}